# There is no clear solution of xcconfig
# https://stackoverflow.com/questions/24236596/edit-xcode-xcconfig-file-and-cocoapods
#
require 'fileutils'


module OPayXCConfigPlugin

class OPayXCConfigInjector


  def inject(installer, config_file_list)
    inject_project config_file_list
  end

  def inject_project(config_file_list)
    config_file = load_configs config_file_list

    plist_set = Set[]

    OPayXCConfigContext.instance.all_projects.uniq.each do |proj_name|

      proj_path = "#{proj_name}.xcodeproj" 
      proj_ref = Xcodeproj::Project.open(proj_path)

    #   proj_ref.build_configuration_list.build_configurations.each do |build_configuration_ref|
        # puts 
        # build_configuration_ref['PRODUCT_BUNDLE_IDENTIFIER'] = '123123'
        proj_ref.targets.each do |target_ref|
            target_ref.build_configurations.each do |project|
                
                config_info = config_file.final_config_with_type ''
                puts "-------------macro_dict------------"
                # puts  config_info.env_dict
                project.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = config_info.env_dict['GZC_BUNDLE_ID']
                project.build_settings['CODE_SIGN_STYLE'] = config_info.env_dict['CODE_SIGN_STYLE']
                project.build_settings['CODE_SIGN_IDENTITY'] = config_info.env_dict['CODE_SIGN_IDENTITY']
                project.build_settings['PROVISIONING_PROFILE_SPECIFIER'] = config_info.env_dict['PROVISIONING_PROFILE_SPECIFIER'] #config_info.env_dict['PROVISIONING_TEAM']
                project.build_settings['DEVELOPMENT_TEAM'] = config_info.env_dict['DEVELOPMENT_TEAM'] 
            end
        end
        proj_ref.save
      end
    #   proj_ref.save
    # end
  end

  

  def load_configs(config_file_list)
    config_file = OPayXCConfigFile.new

    config_file_list.each do |config_file_path|
      next unless File.readable? config_file_path
      config_file.load_config config_file_path
    end
    config_file
  end

end
end


